
package p1;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.geom.Line2D;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Scanner;

import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.Popup;
import java.lang.Object;
import javax.swing.JTextField;

import UFFT_package.UFFT;
import UFFT_package.Decision1.Decision;


public class menu implements ActionListener,ItemListener 
{
	private static final Graphics Graphics = null;
	public static String fname;
	public static int misclassified1=0;
	public static int classified1=0;
	public static int misclassified2=0;
	public static int classified2=0;
	public static int misclassified3=0;
	int changed=0;
	public static int classified3=0;
	public static int tree_no=0;
	public static int[][] confusion_testing1 = new int[10][10];
	public static int[][] confusion_testing2 = new int[10][10];
	public static int num_records;
	public static int[][] confusion_testing3 = new int[10][10];
	JMenuBar menuBar;
	JMenu menu1,menu2, submenu,menuItem43,menuItem33,menuItem13,menuItem23;
	JMenuItem menuItem12;
	JMenuItem menuItem22;
	JMenuItem menuItem32;
	JMenuItem menuItem42;
	
	public JButton b1;
	public static JFrame frame;
	public static JInternalFrame PieFrame;
	//public JComboBox[] cm1=new JComboBox[10];
	public JTextField[] txt1=new JTextField[10];
	public int index=0;
	//String class1=null;
	public static int numDimensions;
	String[] val=new String[10];
	public static String[] test_val=new String[10];
	public static JPanel p1=new JPanel();
	public static JInternalFrame f1;
	JInternalFrame tf1 = null;
	JInternalFrame tf2 = null;
	public int algorithm;
	public static double overhead=0,duration;
 
	
	public static void main(String args[]) throws Exception
	{
		//connect cn=new connect();
		String currentDir = new File(".").getAbsolutePath();
		System.out.println( currentDir ); // input_data folder should be created 
		                                  // in currentDir folder printed by this statement
		fname=new String("input_data\\inp.txt");
		
		long startTime = System.currentTimeMillis();
		
		UFFT v1=new UFFT(fname);
		
		long endTime = System.currentTimeMillis();
		duration=(endTime - startTime);
		for(int i=0;i<Decision.count;i++)
			overhead+=Decision.all_d[i].length();
		menu m=new menu();	
	}
	
	public menu() throws Exception
	{
		//Create the menu bar.
		menuBar = new JMenuBar();
	
		//Build the first menu.
		menu1 = new JMenu("Decision Tree");
		menuBar.add(menu1);
	
		menuItem12 = new JMenuItem("UFFT");
		menuItem12.addActionListener(this);
		menu1.add(menuItem12);
		

		menu1 = new JMenu("Prediction");
		menuBar.add(menu1);
		
		menuItem22 = new JMenuItem("UFFT");
		menuItem22.addActionListener(this);
		menu1.add(menuItem22);
		

		menu1 = new JMenu("Apply Test Data");
		menuBar.add(menu1);
		
		menuItem32 = new JMenuItem("UFFT");
		menuItem32.addActionListener(this);
		menu1.add(menuItem32);
		


		menu1 = new JMenu("Statistics Details");
		menuBar.add(menu1);
		
		
		menuItem42 = new JMenuItem("UFFT");
		menuItem42.addActionListener(this);
		menu1.add(menuItem42);
		
		frame=new JFrame("Classification Menu");
		frame.setJMenuBar(menuBar);
		frame.setVisible(true);
		frame.setSize(900,700);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	

	@SuppressWarnings("deprecation")
	public void actionPerformed(ActionEvent ae)
	{

		/*-------------------Decision Tree Menu----------------------*/
	
		if(ae.getSource()==menuItem12)
		{
			if(tf1!=null)
				frame.remove(tf1);
			if(f1!=null)
				frame.remove(f1);
			if(PieFrame!=null)
				frame.remove(PieFrame);
 	
			//algorithm=2;
			tf1=(JInternalFrame) UFFT_package.Decision1.Decision.print();
			if(tf1==null)
			{
				JLabel l=new JLabel("Hoeffding Bound not satisfied. Tree not Created...!");
				frame.setVisible(false);
				frame.add(l);
				frame.setVisible(true);
			}
			else
				frame.add(tf1);
			
		}
		
		/*-------------------Predict Class Label MENU------------------------*/
		if(ae.getSource()==menuItem22)
		{
			if(tf1!=null)
			{
				tf1.removeAll();
				frame.remove(tf1);
			}
			if(f1!=null)
			{
				f1.removeAll();
				frame.remove(f1);
			}
			if(PieFrame!=null)
				frame.remove(PieFrame);
			//algorithm=2;
			t1();
		}
		
	
		/*-------------------TEST DATA MENU------------------------*/
		if(ae.getSource()==menuItem32)
		{
			if(tf1!=null)
			{
				tf1.removeAll();
				frame.remove(tf1);
			}
			if(f1!=null)
			{
				f1.removeAll();
				frame.remove(f1);
			}
			if(PieFrame!=null)
				frame.remove(PieFrame);
			algorithm=2;
			misclassified2=0;
            classified2=0;
			testing("input_data\\test_data1.txt");
		}
		
		/*---------Statistics-------- */
		
		if(ae.getSource()==menuItem42)
		{
			System.out.println("UFFT statistics");
			if(tf1!=null)
			{
				tf1.removeAll();
				frame.remove(tf1);
			}
			if(f1!=null)
			{
				f1.removeAll();
				frame.remove(f1);
			}
	
			if(PieFrame!=null)
				frame.remove(PieFrame);
			UFFT_package.chart1.PieChartExample pie=new UFFT_package.chart1.PieChartExample();
			PieFrame=UFFT_package.chart1.PieChartExample.f;
			PieFrame.setVisible(true);
			frame.add(UFFT_package.chart1.PieChartExample.f);
		}
		
		
		/*---------proceed button-- */
		if(ae.getSource()==b1)
		{
			String class1;
			int flag=0;
			class1=new String("");
			for(int i=0;i<numDimensions;i++)
			{
				txt1[i].setEditable(false);
			}
			
			// Read from all text boxes and put it in val
			for(int i = 0;i<numDimensions;i++)
			{
				JTextField textField = txt1[i];
				// Try to parse input string, if it is double, keep it as it is, otherwise make it zero.
				// Note that we do not store parsed double here. 
				try
				{
					Double dIgnore = Double.parseDouble(textField.getText());
					val[i] = textField.getText(); // Our code can deal only with Doubles.
				}
				catch(Exception ignore)
				{
					val[i] = "0";
					flag=1;
					//txt1[i].setText("0");
				}
			}
				
			// if(algorithm==2)
			{
				UFFT_package.Example1.Example testData;
				testData=new UFFT_package.Example1.Example(numDimensions);
				
			/*	for(int i=0;i<numDimensions;i++)
					System.out.println("****"+val[i]);
			*/	
				testData.setValues(val);
				class1=new String("Class Label :");
				
				
				class1+=UFFT_package.Decision1.Decision.traverse(testData);
			}
			Font f=new Font("Times New Roman",20,20);
			
			JLabel l1;
			if(flag==0)
				l1=new JLabel(class1);
			else
				l1=new JLabel("Error: Incorrect Input ");
			class1=new String("");
			b1.setVisible(false);
			l1.setLocation((60*numDimensions),150);
			l1.setSize(500,30);
			l1.setFont(f); 
			f1.setVisible(false);
			f1.add(l1);
			f1.setVisible(true);
		}
	}

	private void testing(String in_file) {
		if(p1!=null)
		{	
			p1.removeAll();
		}
		
		String class1;
		f1=null;
		f1=new JInternalFrame("Testing Data For UFFT Algo...");
		
		ArrayList test=new ArrayList();
		//Example test_example;
		int c1,c2;
		
		String temp;
        JLabel l1,result;
		
		for(int i=0;i<Decision.num_classes;i++)
		{
			for(int j=0;j<Decision.num_classes;j++)
			{
					 confusion_testing2[i][j]=0;
				  
			}
				
		}
		f1.setLayout(null);
		//File file=new File();
		try
		{
			Scanner sc=new Scanner(new File(in_file));
			
			num_records=sc.nextInt();
			int i;
			JLabel t1=new JLabel("The Contents of the Test data are As ::: ");
			t1.setLocation(60,10);
			t1.setSize(200,60);
			f1.add(t1);
			JLabel t2=new JLabel("Predicted Class Label");
			t2.setLocation(450,10);
			t2.setSize(200,60);
			f1.add(t2);
			JLabel t3=new JLabel("Result");
			t3.setLocation(650,10);
			t3.setSize(120,60);
			t3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
			f1.add(t3);
			f1.pack();
			System.out.println(num_records);
			for(int j=0;j<num_records;j++)
			{
                System.out.println(Decision.num_dimensions);   
				for( i = 0;i<Decision.num_dimensions;i++)
				{
					test_val[i]=sc.next();
                    t1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
					JLabel lb=new JLabel(test_val[i]);	
					lb.setLocation((100*i/2),50+(20*j));
					lb.setSize(80,40);
					f1.add(lb);
					
				}
				
				
				{
					UFFT_package.Example1.Example test_example;
					test_example=new UFFT_package.Example1.Example(numDimensions);
					test_example.setValues(test_val);
					temp=sc.next();
					JLabel cb=new JLabel(temp);	
					cb.setLocation((100*i/2),50+(20*j));
					cb.setSize(80,40);
					f1.add(cb);
					c1=test_example.lookup_class(temp);
					test_example.setMy_class(c1);
					
			
					class1=UFFT_package.Decision1.Decision.traverse(test_example);
					l1=new JLabel(class1);
					//class1=new String("");
			
					l1.setLocation(450,50+(20*j));
					l1.setSize(80,40);
					l1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
					f1.add(l1);
						
					c2=test_example.lookup_class(class1);
					confusion_testing2[c1][c2]++;
					if(c1==c2)
					{
						result=new JLabel("Classified Accurately");
						result.setLocation(650,50+(20*j));
						result.setSize(180,40);
						f1.add(result);
						classified2++;
					}
					else
					{
						result=new JLabel("Misclassified ");
						result.setLocation(650,50+(20*j));
						result.setSize(180,40);
						f1.add(result);
						misclassified2++;
					}
				
				}
			    
				//Font f=new Font("Times New Roman",20,20);
				
								
			}
            
			frame.add(f1);
			frame.setResizable(true);
			frame.setSize(900,600);
			f1.setVisible(true);
		}
		catch(Exception e)
		{
				e.printStackTrace();
		}	
		
	}

	public void t1()
	{
		if(p1!=null)
		{	
			p1.removeAll();
		}
		f1=null;
		b1=new JButton("Proceed");
		f1=new JInternalFrame("Select Input");
		String temp;
		f1.setLayout(null);
		
		File file=new File("input_data\\predict_data.txt");
		
		try
		{
			Scanner sc=new Scanner(file);
			temp=sc.next();
			while(!temp.equals("dimensions"))
			{
				temp=sc.next();
			}
			System.out.println(temp);	
			numDimensions = sc.nextInt();
			int i=0;
			for(i = 0;i<numDimensions;i++)
			{
				temp=sc.next();
				temp=temp+":";
				
				JLabel t1=new JLabel(temp);
				t1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
						
				if(i<4)
				{
					t1.setLocation((190*i),30);
					t1.setSize(80,40);
					
					txt1[i]= new JTextField();
					txt1[i].addActionListener(this);
					txt1[i].setLocation((80+(190*i)),40);
					txt1[i].setSize(80,25);
					txt1[i].setVisible(true);
				}
				else
				{
					int k=i-3;
					t1.setLocation((190*k),80);
					t1.setSize(80,40);
					
					txt1[i]= new JTextField();
					txt1[i].addActionListener(this);
					txt1[i].setLocation((80+(190*k)),80);
					txt1[i].setSize(80,25);
					txt1[i].setVisible(true);
				}
				f1.add(t1);
				f1.add(txt1[i]);
			}
			f1.setSize(800,400);
			b1.addActionListener(this);
			b1.setLocation((60*i),150);
			b1.setSize(100,30);
			f1.add(b1);
			frame.add(f1);
			frame.setResizable(true);
			frame.setSize(900,700);
			f1.setVisible(true);
			
			//System.out.println(testdata.value)
		}
		
		catch(Exception e)
		{
				e.printStackTrace();
		}	
	}
	
	@Override
	public void itemStateChanged(ItemEvent arg0) 
	{
		// TODO Auto-generated method stub
		
	}

	
}

